﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Shop
{
    public static class DataTableExt
    {
        #region 把DataTable转为List
        public static List<Dictionary<string,object>> ToList(this DataTable dt)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (DataRow dataRow in dt.Rows)
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>(); //实例化一个参数集合
                foreach (DataColumn dataColumn in dt.Columns)
                {
                    string value;
                    if (dataRow[dataColumn.ColumnName] == DBNull.Value)
                    { value = ""; }
                    else
                    { value = dataRow[dataColumn.ColumnName].ToString(); }
                    dictionary.Add(dataColumn.ColumnName, value);
                }
                list.Add(dictionary);
            }
            return list;
        }
        #endregion
    }
}